﻿using System;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Data;
using nRoute.Components;
using System.ComponentModel;
using System.Windows.Interactivity;

namespace nRoute.Behaviors
{
    [ContentProperty("Parameters")]
    public abstract class NavigateActionBase
        : NavigationHandlerActionBase
    {

#region Constants and Variables

        public static readonly DependencyProperty UrlProperty = 
            DependencyProperty.Register("Url", typeof(string), typeof(NavigateActionBase), 
            new PropertyMetadata(null));

        public static readonly DependencyProperty ParametersProperty =
            DependencyProperty.Register("Parameters",
#if (WP7)
            typeof(ParametersCollection), typeof(NavigateActionBase), null);
#else
            typeof(DependencyParametersCollection), typeof(NavigateActionBase), null);
#endif
            
        public static readonly DependencyProperty SiteAreaProperty =
            DependencyProperty.Register("SiteArea", typeof(string), typeof(NavigateActionBase),
            new PropertyMetadata(null));

#endregion

#region Properties

        [Category("Common Properties")]
#if WP7
        [TypeConverter(typeof(nRoute.Components.TypeConverters.ConvertFromStringConverter))]
#endif
        public string Url
        {
            get { return Convert.ToString(GetValue(UrlProperty)); }
            set { SetValue(UrlProperty, value); }
        }

#if (WP7)

        [Category("Common Properties")]
        public ParametersCollection Parameters
        {
            get 
            {
                // lazy load, not the best idea but with DP's - but this way we can avoid unnecessary creation of parameters
                if (GetValue(ParametersProperty) == null)
                {
                    SetValue(ParametersProperty, new ParametersCollection());
                }
                return (ParametersCollection)GetValue(ParametersProperty); 
            }
            set { SetValue(ParametersProperty, value); }
        }

#else

        [Category("Common Properties")]
        public DependencyParametersCollection Parameters
        {
            get 
            {
                // lazy load, not the best idea but with DP's - but this way we can avoid unnecessary creation of parameters
                if (GetValue(ParametersProperty) == null)
                {
                    SetValue(ParametersProperty, new DependencyParametersCollection());
                }
                return (DependencyParametersCollection)GetValue(ParametersProperty);
            }
            set { SetValue(ParametersProperty, value); }
        }        

#endif

        [Category("Common Properties")]
        public string SiteArea
        {
            get { return Convert.ToString(GetValue(SiteAreaProperty)); }
            set { SetValue(SiteAreaProperty, value); }
        }

#if WP7

        [Category("Common Properties")]
#if (!WP7)
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
#endif
        public Binding UrlBinding
        {
            get { return GetBinding(UrlProperty); }
            set { SetBinding<string>(UrlProperty, value); }
        }

        [Category("Common Properties")]
#if (!WP7)
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
#endif
        public Binding ParametersBinding
        {
            get { return GetBinding(ParametersProperty); }
            set { SetBinding<ParametersCollection>(ParametersProperty, value); }
        }

        [Category("Common Properties")]
#if (!WP7)
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
#endif
        public Binding SiteAreaBinding
        {
            get { return GetBinding(SiteAreaProperty); }
            set { SetBinding<string>(SiteAreaProperty, value); }
        }

#endif

#endregion

    }
}
